home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / winftp.zip / WNFTPSRC.ZIP / WS_IP.C < prev    next >
C/C++ Source or Header  |  1993-12-20  |  5KB  |  145 lines

  1. /*
  2.   MODULE: WinFTP.C
  3.  
  4.   Based on code published in UNIX Network Programming by W. Richard Stevens
  5.  
  6.   These are common routines that will be needed by many different programs
  7.   and should not be modified for a specific instance.
  8. */
  9.  
  10. #include "ws_glob.h"
  11. #include "winftp.h"
  12. #include <stdlib.h>
  13.  
  14. SOCKET connectTCP(char *host,char *service)
  15. {
  16.   return connectsock(host,service,"tcp");
  17. }
  18.  
  19. SOCKET connectUDP(char *host,char *service)
  20. {
  21.   return connectsock(host,service,"udp");
  22. }
  23.  
  24. //*********************************************************************
  25. //*********************************************************************
  26. GetDefaultService (LPSTR lpServ)
  27. {
  28.   if (lstrcmpi (lpServ, "ftp")==0) return 21;
  29.   return -1;
  30. }
  31.  
  32. //*********************************************************************
  33. //*********************************************************************
  34. GetDefaultProtocol (LPSTR lpProto)
  35. {
  36.   if (lstrcmpi (lpProto, "tcp")==0) return 6;
  37.   return -1;
  38. }
  39.  
  40. //*********************************************************************
  41. //*********************************************************************
  42. SOCKET connectsock (LPSTR host, LPSTR service, LPSTR protocol)
  43. {
  44.   struct hostent *pHostEntry;      // pointer to host entry
  45.   struct servent *pServiceEntry;   // pointer to service entry
  46.   struct protoent *pProtoEntry;    // pointer to protocol entry
  47.   SOCKET sSocket;                  // socket
  48.   int nSocketType;                 // socket type
  49.   short nProtocol;                 // protocol value
  50.  
  51.   // initialize socket address structure
  52.   // get service port number from name
  53.   memset((char *)&saSockAddr,0,sizeof(saSockAddr));
  54.   saSockAddr.sin_family=AF_INET;
  55.   if (pServiceEntry=getservbyname(service,protocol))
  56.     saSockAddr.sin_port=pServiceEntry->s_port;
  57.   else if ((saSockAddr.sin_port=htons((u_short)atoi(service)))==0) 
  58.   {
  59.     DoPrintf ("can't get \"%s\" service entry, will use default",service);
  60.     saSockAddr.sin_port = htons ((u_short) GetDefaultService (service));
  61.   }
  62.   // map host to ip, allow ip
  63.   if (pHostEntry=gethostbyname(host))
  64.   {
  65.     memcpy((char *)&saSockAddr.sin_addr,pHostEntry->h_addr,pHostEntry->h_length);
  66.   }
  67.   else if((saSockAddr.sin_addr.s_addr=inet_addr(host))==INADDR_NONE) 
  68.   {
  69.     DoPrintf("can't get \"%s\" host entry",host);
  70.     return INVALID_SOCKET;
  71.   }
  72.   // map protocol name to protocol number
  73.   // use protocol to choose socket type
  74.   if ((pProtoEntry=getprotobyname(protocol))==0) 
  75.   {
  76.     DoPrintf("can't get \"%s\" protocol entry, will use default", protocol);
  77.     nProtocol = GetDefaultProtocol (protocol);
  78.     if (nProtocol==-1)  return INVALID_SOCKET;
  79.   }
  80.   else 
  81.   {
  82.       nProtocol = pProtoEntry->p_proto;
  83.   }
  84.   // DoPrintf ("Protocol is %s, value %d", protocol, pProtoEntry->p_proto);
  85.   if (strcmp (protocol,"udp")==0)
  86.     nSocketType = SOCK_DGRAM;
  87.   else
  88.     nSocketType = SOCK_STREAM;
  89.   // allocate a socket
  90.   sSocket = socket (AF_INET, nSocketType, nProtocol);
  91.   if (sSocket==INVALID_SOCKET) 
  92.   {
  93.     ReportWSError("create socket",WSAGetLastError());
  94.     return INVALID_SOCKET;
  95.   }
  96.  
  97.   memcpy ((LPSTR)&saSockAddr1, (LPSTR)&saSockAddr, sizeof (saSockAddr));
  98.   saSockAddr1.sin_port=htons(20);
  99.  
  100.   // connect the socket
  101.   if (connect(sSocket, (struct sockaddr *)&saSockAddr, sizeof(saSockAddr))
  102.       ==SOCKET_ERROR) {
  103.     DoPrintf("Can't connect to %s %s service.", host, service);
  104.     ReportWSError (NULL,WSAGetLastError());
  105.     return INVALID_SOCKET;
  106.   }
  107.  
  108.   DoPrintf ("Connected to %s port %u",
  109.     inet_ntoa (saSockAddr.sin_addr), ntohs (saSockAddr.sin_port));
  110.  
  111.   return sSocket;
  112. }
  113.  
  114. //*********************************************************************
  115. //*********************************************************************
  116. int sendstr (SOCKET sSocket, LPSTR pBuf, int nBytesToWrite, int *lpnRetCode)
  117. {
  118.   int nBytesLeft, nBytesWritten, nError;
  119.  
  120.   nBytesLeft = nBytesToWrite;
  121.   
  122.   // while we haven't written enough or transfer has not been aborted
  123.   // send out the data 
  124.   while (nBytesLeft > 0)
  125.   {                   
  126.     nBytesWritten = send (sSocket, pBuf, nBytesLeft,0); // write what we can
  127.     if (nBytesWritten==SOCKET_ERROR) 
  128.     {
  129.       ReportWSError ("Send", nError = WSAGetLastError());
  130.       *lpnRetCode = (bAborted) ? FTP_ABORT : FTP_ERROR;
  131.       return nBytesWritten;  // error occured
  132.     }
  133.     nBytesLeft -= nBytesWritten;                 // count what we wrote
  134.     pBuf   += nBytesWritten;                     // adjust buffer pointer
  135.     if (bAborted)
  136.     {
  137.        break;
  138.     }
  139.   }
  140.   if (bAborted) *lpnRetCode = FTP_ABORT;
  141.   else *lpnRetCode = FTP_COMPLETE;
  142.   return (nBytesToWrite - nBytesLeft);       // return count of bytes written
  143. }
  144.  
  145.